package 寒假刷题;

import java.util.LinkedList;
import java.util.List;

public class 全排列 {
    List<List<Integer>> lists = new LinkedList<>();
    List<Integer> list = new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {
          int n = nums.length;
          backtrace(nums,n);
          return lists;
    }

    private void backtrace(int[] nums, int n) {
        if (list.size()==n)
            lists.add(new LinkedList<>(list));
        for (int i = 0; i < n; i++) {
            if (list.contains(nums[i]))
                continue;
            list.add(nums[i]);
            backtrace(nums,n);
            list.remove(list.size()-1);
        }
    }
}
